var orderby = "order by bzn.id";
var pageSize = 2;
var pageflag = "comment";
var tabflag = '';
var parentid = '';
var editor ; 
var sum = 0 ;
/*$(document).ready(function() {
	alert("===0000");
	initTab('pri');
});*/
// 翻页响应
function pageselectCallback(page_id) {
	if(pageflag=='comment'){
		initData(page_id , tabflag);
	}else if(pageflag=='mycomment'){
		showCommentDetail(parentid,page_id );
	}else if(pageflag=='commentdraft'){
		initDraft(page_id , tabflag);
	}
	
}

function initData(pageindx , flag) {
	tabflag = flag;
	var pageCount = "";
	
	
	var pckeyword = $("#"+flag+"_pckeyword").attr("value");
	var pcuserid ="";
	
	if(tabflag=='pub'){
		pcuserid = "";
	}else if(tabflag=='pri'){
		pcuserid =  $("#"+tabflag+"_pcuserid").attr("value");
	}
	if(pckeyword=="key word"){
		pckeyword="";
	}
	
	
	
	$
			.ajax({
				contentType : "application/x-www-form-urlencoded; charset=utf-8",
				type : "POST",// 用POST方式传输
				dataType : "json",// 数据格式:JSON
				url : 'findComments.action',// 目标地址
				async : false,// 作用是防止在ajax成功调用之前就调用$("#Pagination").pagination,这个时候数据个数还没有初始化
				data : "page.page=" + (pageindx + 1) + "&tableData.orderby="
						+ escape(orderby) + "&page.pageSize=" + pageSize+"&bzn_comment.title="+pckeyword+"&bzn_comment.usr_user.id="+pcuserid,
				beforeSend : function() {
					$("#divload").show();
					$("#Pagination").hide();
				},// 发送数据之前
				complete : function() {
					$("#divload").hide();
					if(pageCount==0){
						$("#Pagination").hide();
					}else{
						$("#Pagination").show();
					}
					
				},// 接收数据完毕
				error : function() {
					alert("error");
				},
				success : function(json) {
					$("#"+tabflag+"_mycommentlist").empty();
					$("#"+flag+"_commentlist").empty();
					$("#"+flag+"_commentlist").show();
					pageCount = json[0];
					if (json == "") {
						$("#divload").hide();
						dis();
						return false;
					}

					$
							.each(
									json,
									function(i, n) {
										
										if (i>0) {
											
											var str = "<h3 class='comp'>"
												+"Title:"+n[0]+"   owner:"+n[2]+"<a class='link'  id='aview"+n[3]+"'   onclick='showCommentDetail("+n[3]+",0 )' >view</a>&nbsp;";
											
											
											if(tabflag=='pri'){
												str = str +"<a class='link'  id='aview"+n[3]+"'   onclick='modifyCommentDetail("+n[3]+",0)' >modify</a>";
											}
											str = str +"</h3>";
											$("#"+flag+"_commentlist").append(str);
										/*	<a class='link'style='display:none;' id='ahide"+n[3]+"' onclick='showOrHideTable("+n[3]+")'>hide</a>
											<div  id='hidden"+n[3]+"' class='chat' style='display:none'><textarea rows='5' cols='100'>"+n[2]+"</textarea>" +
											
											"<a class='link' onclick='showCommentDetail("+n[3]+",0)'>Comment</a></div>
*/										}
									});
					
				}// end of success

			});
	if (pageCount != 0) {
		$("#Pagination").pagination(pageCount, {
			callback : pageselectCallback,
			prev_text : '<< 上一页',
			next_text : '下一页 >>',
			items_per_page : pageSize,
			num_display_entries : 10,
			current_page : pageindx,
			num_edge_entries : 2,
			link_to : "view/user/pindex.jsp#"

		});
	}
}


function showOrHideTable(str){
	if ($('#hidden'+str).is(":hidden")) {
		$("#hidden"+str).show();
		$("#aview"+str).hide();
		$("#ahide"+str).show();
		
	}else{
		$("#hidden"+str).hide();
		$("#aview"+str).show();
		$("#ahide"+str).hide();
	}
	
}
function operationCommentDetail(id,operationflag , action , data){
	
	$.ajax({
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		type : "POST",// 用POST方式传输
		dataType : "json",// 数据格式:JSON
		url : action+".action",// 目标地址
		data : data+"="+id,
		async: false, 
		error : function() {
			alert("jsonerror");
		},
		success : function(json) {
			$("#"+tabflag+"_pcsearch").hide();
			if(operationflag=='view'){
				$("#"+tabflag+"_mycommentlist").empty();
				$("#"+tabflag+"_pctitle").attr("value",json[0][0]);
				$("#"+tabflag+"_pccontentview").html(json[0][1]);
				$("#"+tabflag+"_pcparentid").attr("value",json[0][2]);
				//$("#"+tabflag+"_pcuserid").attr("value",json[0][3]);
				$("#"+tabflag+"_pctitle").attr("disabled","disabled");
				$("#"+tabflag+ "_pcselectcategory").attr("disabled","disabled");
				$("#"+tabflag+ "_pccontentview").attr("disabled","disabled");
				$("#"+tabflag+"_addlink").hide();
				$("#"+tabflag+"_pccontentview").show();
				$("#"+tabflag+"_enlink").show();
				$("#"+tabflag+"_molink").hide();
				$("#"+tabflag+"_commentlist").hide();
				$("#"+tabflag+"_addcomment").show();
				$("#"+tabflag+ "_pcselectcategory option[value='"+json[0][5]+"']").attr('selected',true);
				$("#"+tabflag+"_textarea").hide();
				$("#"+tabflag+"_textareaview").show();
			}else if(operationflag=='modify'){
				$("#"+tabflag+"_addcomment").show();
				$("#"+tabflag+"_mycommentlist").empty();
				editor.html(json[0][1]);
				$("#"+tabflag+"_pctitle").attr("value",json[0][0]);
				$("#"+tabflag+"_pcparentid").attr("value",json[0][2]);
				//$("#"+tabflag+"_pcuserid").attr("value",json[0][3]);
				$("#"+tabflag+"_addlink").hide();
				$("#"+tabflag+"_enlink").hide();
				$("#"+tabflag+"_molink").show();
				$("#"+tabflag+ "_pcselectcategory option[value='"+json[0][5]+"']").attr('selected',true);
				$("#"+tabflag+"_textareaview").hide();
				$("#"+tabflag+"_textarea").show();
			}else if(operationflag=='modifyCommentDraft'){
				$("#"+tabflag+ "_pcselectcategory option[value='"+json[0][5]+"']").attr('selected',true);
				$("#"+tabflag+"_pctitle").attr("value",json[0][0]);
				editor.html(json[0][1]);
				$("#"+tabflag+"_pccontentview").attr("value",json[0][1]);
				$("#"+tabflag+"_textareaview").hide();
				$("#"+tabflag+"_textarea").show();
				$("#"+tabflag+"_pcid").attr("value",json[0][2]);
				
			}
			
			
		}
	});
}
function modifyCommentDetail(id ){
	KindEditor.remove("#"+tabflag+"_pccontent");
	getKindEditor();
	operationCommentDetail(id,"modify" , "findCommentById" , "bzn_comment.id");
}
function modifyCommentDraftDetail(id ){
	KindEditor.remove("#"+tabflag+"_pccontent");
	getKindEditor();
	$("#mydraft").show();
	operationCommentDetail(id,"modifyCommentDraft" , "findCommentDraftById" , "bzn_commentdraft.id");
}
function showCommentDetail(id,pageindx ){
	pageflag = "mycomment";
	parentid = id;
	operationCommentDetail(id,"view" ,"findCommentById" , "bzn_comment.id");
	
	var pageCount = "";
	$.ajax({
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		type : "POST",// 用POST方式传输
		dataType : "json",// 数据格式:JSON
		url : "findComments.action",// 目标地址
		data : "page.page=" + (pageindx + 1) + "&tableData.orderby="
		+ escape(orderby) + "&page.pageSize=" + pageSize+"&bzn_comment.id="+id,
		async: false, 
		beforeSend : function() {
			$("#divload").show();
			$("#Pagination").hide();
		},// 发送数据之前
		complete : function() {
			$("#divload").hide();
		
			if(pageCount==0){
				$("#Pagination").hide();
			}else{
				$("#Pagination").show();
			}
			
			
		},// 接收数据完毕
		error : function() {
			alert("error");
		},
		success : function(json) {
			$("#"+tabflag+"_commentlist").empty();
			$("#"+tabflag+"_mycommentlist").empty();
			$("#"+tabflag+"_mycommentlist").show();
			pageCount = json[0];
			
			 if (json == "") {
				$("#divload").hide();
				dis();
				return false;
			}
			$.each(json,function(i, n) {
				if(i>0){
						$("#"+tabflag+"_mycommentlist").append("<br><h3 class='listtitle'>"
							+"writer:"+n[2] +
									"</h3><div  class='listchat' ><textarea rows='5' cols='100' disabled='disabled' >"+n[1]+"</textarea>" +
																	
																	"</div>" );
									
							
				}
					});
			
		}
	});
	if (pageCount != 0) {
		$("#Pagination").pagination(pageCount, {
			callback : pageselectCallback,
			prev_text : '<< 上一页',
			next_text : '下一页 >>',
			items_per_page : pageSize,
			num_display_entries : 10,
			current_page : pageindx,
			num_edge_entries : 2,
			link_to : "view/user/pindex.jsp#"

		});
		
	}
	
}

function addCommentUI(){
	$('#'+tabflag+'_comment').slideDown();
	
}

function cancelCommentUI(){
	$('#'+tabflag+'_comment').slideUp();
	$('#'+tabflag+'_pccomment').attr("value","");
	
}


function submitComment(){
	if($("#"+tabflag+"_pccomment").attr("value")!=''){
		insertData("comment,parentid,userid",tabflag+"_pc","addComment","bzn_comment.content,bzn_comment.parentId,bzn_comment.usr_user.id" );
		$("#"+tabflag+"_comment").hide();
		$("#"+tabflag+"_pccomment").attr("value","");
		showCommentDetail($("#"+tabflag+"_pcparentid").attr("value"),0);
	}else{
		
		alert("cannot be null");
	}
	
	
}

function submitmypcmodify(){
	$("#"+tabflag+"_pccontentview").attr("value",editor.html());
	if($("#"+tabflag+"_pctitle").attr("value")!=''&&$("#"+tabflag+"_pccontentview").attr("value")!=''){
		insertData("parentid,title,contentview,userid,selectcategory",tabflag+"_pc","modifyComment","bzn_comment.id,bzn_comment.title,bzn_comment.content,bzn_comment.usr_user.id,bzn_comment.bsc_category.id" );
		$("#"+tabflag+"_pctitle").attr("value","");
		$("#"+tabflag+"_pccontentview").attr("value","");
		$("#"+tabflag+"_pckeyword").attr("value","key word");
		$("#"+tabflag+"_addlink").show();
		$("#"+tabflag+"_molink").hide();
		$("#"+tabflag+"_enlink").hide();
		$("#"+tabflag+"_commentlist").show();
		KindEditor.remove("#"+tabflag+"_pccontent");
		initTab(tabflag);
		editor.html('');
	}else{
		alert("cannot be null");
	}
}

function initTab(flag){
	//pri_pcselectcategory
	//$("#pri_addcomment").show();
	pageflag = "comment";
	tabflag = flag;
	selectData(flag+"_pcselectcategory","","findAllCategory","" );
	$("#"+tabflag+"_pctitle").attr("value","");
	$("#"+tabflag+"_pccontent").attr("value","");
	$("#"+tabflag+"_pctitle").attr("disabled","");
	$("#"+tabflag+"_pccontent").attr("disabled","");
	$("#"+tabflag+"_pckeyword").attr("value","key word");
	$("#"+tabflag+"_addlink").show();
	$("#"+tabflag+"_molink").hide();
	$("#"+tabflag+"_enlink").hide();
	$('#'+tabflag+'_comment').hide();
	$('#'+tabflag+'_pccomment').attr("value","");
	$('#draft_pcsearch').hide();
	$('#mydraft').hide();
	initData(0,flag);
	$("#"+tabflag+"_addcomment").hide();
	$("#"+tabflag+"_pcsearch").show();
	getKindEditor();
	
	
	
		
}


function mypcsubmit(){
	$("#"+tabflag+"_pccontentview").attr("value",editor.html());
	if($("#"+tabflag+"_pctitle").attr("value")!=''&&editor.html()!=''){
		insertData("title,contentview,userid,selectcategory",tabflag+"_pc","addComment","bzn_comment.title,bzn_comment.content,bzn_comment.usr_user.id,bzn_comment.bsc_category.id" );
		$("#"+tabflag+"_pctitle").attr("value","");
		$("#"+tabflag+"_pccontentview").attr("value","");
		$("#"+tabflag+"_pckeyword").attr("value","key word");
		$("#"+tabflag+"_commentlist").show();
		editor.html('');
		if(tabflag=='draft'){
			initDraft(0,tabflag);
		}else{
			initTab(tabflag);
		}
		
	}else{
		alert("cannot be null");
	}
	
	
}
function searchPc(){
	initData(0,tabflag);
}

function initDraft(pageindx , flag){
	pageflag = "commentdraft";
	selectData(flag+"_pcselectcategory","","findAllCategory","" );
	$('#draft_pcsearch').show();
	tabflag = flag;
	$("#tab").hide();
	$("#mydraft").hide();
	getKindEditor();
	pageflag = "commentdraft";
	var id = $("#"+tabflag+"_pcuserid").attr("value");
	var pckeyword = $("#"+flag+"_pckeyword").attr("value");
	
	if(pckeyword=="key word"){
		pckeyword="";
	}
	var pageCount = "";
	$.ajax({
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		type : "POST",// 用POST方式传输
		dataType : "json",// 数据格式:JSON
		url : "findCommentDrafts.action",// 目标地址
		data : "page.page=" + (pageindx + 1) + "&tableData.orderby="
		+ escape(orderby) + "&page.pageSize=" + pageSize+"&bzn_commentdraft.usr_user.id="+id+"&bzn_commentdraft.title="+pckeyword,
		async: false, 
		beforeSend : function() {
			$("#divload").show();
			$("#Pagination").hide();
		},// 发送数据之前
		complete : function() {
			$("#divload").hide();
		
			if(pageCount==0){
				$("#Pagination").hide();
			}else{
				$("#Pagination").show();
			}
			
			
		},// 接收数据完毕
		error : function() {
			alert("error");
		},
		success : function(json) {
			$("#commentdraftlist").empty();
			$("#commentdraftlist").show();
		
			pageCount = json[0];
			
			 if (json == "") {
				$("#divload").hide();
				dis();
				return false;
			}
			$.each(json,function(i, n) {
				
				if(i>0){
						$("#commentdraftlist").append("<h3 class='comp'> title:"+n[0] 
							+" writer:"+n[2] +
									"<a class='link'  id='adraftview"+n[3]+"'   onclick='modifyCommentDraftDetail("+n[3]+",0)' >modify</a></h3>" );
					
							
				}
					});
			
		}
	});
	if (pageCount != 0) {
		$("#Pagination").pagination(pageCount, {
			callback : pageselectCallback,
			prev_text : '<< 上一页',
			next_text : '下一页 >>',
			items_per_page : pageSize,
			num_display_entries : 10,
			current_page : pageindx,
			num_edge_entries : 2,
			link_to : "view/user/pindex.jsp#"

		});
		
	}
}


function mypcdraftsubmit(){
	alert(editor.html());
	$("#"+tabflag+"_pccontentview").attr("value",editor.html());
	if($("#"+tabflag+"_pctitle").attr("value")!=''&&$("#"+tabflag+"_pccontentview").attr("value")!=''){
		insertData("title,contentview,userid,selectcategory",tabflag+"_pc","addCommentDraft","bzn_commentdraft.title,bzn_commentdraft.content,bzn_commentdraft.usr_user.id,bzn_commentdraft.bsc_category.id" );
		$("#"+tabflag+"_pctitle").attr("value","");
		$("#"+tabflag+"_pccontentview").attr("value","");
		$("#"+tabflag+"_pckeyword").attr("value","key word");
		$("#"+tabflag+"_commentlist").show();
		editor.html('');
		initData(0,tabflag);
	}else{
		alert("cannot be null");
	}
}

function mypcdraftmodify(str){
	
	$("#"+tabflag+"_pccontentview").attr("value",editor.html());
	$("#"+tabflag+"_pcflag").attr("value",str);
	if($("#"+tabflag+"_pctitle").attr("value")!=''&&$("#"+tabflag+"_pccontentview").attr("value")!=''){
		insertData("id,title,contentview,userid,flag,selectcategory",tabflag+"_pc","modifyCommentDraft","bzn_commentdraft.id,bzn_commentdraft.title,bzn_commentdraft.content,bzn_commentdraft.usr_user.id,bzn_commentdraft.flag,bzn_commentdraft.bsc_category.id" );
		if(str==0){
			$("#"+tabflag+"_pctitle").attr("value","");
			$("#"+tabflag+"_pccontentview").attr("value","");
			KindEditor.remove("#"+tabflag+"_pccontent");
			initDraft(0,tabflag);
			editor.html('');
		}
		//$("#"+tabflag+"_pckeyword").attr("value","key word");
		$("#mydraft").hide();
		
	}else{
		alert("cannot be null");
	}
}

function addToMyQuestion(){
	mypcdraftmodify(1);
	mypcsubmit();
}

function cancelCommentDraftUI(){
	$('#draft_pcsearch').show();
	$("#"+tabflag+"_pctitle").attr("value","");
	$("#"+tabflag+"_pccontentview").attr("value","");
	$("#mydraft").hide();
}

function searchDraft(){
	initDraft(0 , tabflag);
}

function getKindEditor(){
	
	$.getScript('kindeditor/kindeditor.js', function() {
		 editor = KindEditor.create('#'+tabflag+'_pccontent', {
				cssPath : 'kindeditor/plugins/code/prettify.css',
				uploadJson : 'upload_json.jsp',
				width : 580,
				height : 400,
				//fileManagerJson : '../jsp/file_manager_json.jsp',
					items : [
		'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
		'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
		'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
		'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', 
		'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
		'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
		'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
		'anchor', 'link', 'unlink', '|', 'about'
	],
				allowFileManager : false,
	});
		
	});
	
}